----------------------------------------------------------------------------------------------------------------------------------
local npc_everything_data = Class(function(self, inst)
    self.inst = inst
    -- self.OnLoad_Fn_list={}
    self.DataTable = {}
    self.inst.npc_everything_data = self
end,
nil,
{
    -- canbemilked = onHearthstone,
	-- caninteractwith = onHearthstone,
})

function npc_everything_data:SaveData(DataName_Str,theData)
    if DataName_Str then
        self.DataTable[DataName_Str] = theData
    end
end

function npc_everything_data:ReadData(DataName_Str)
    if DataName_Str then
        if self.DataTable[DataName_Str] then
            return self.DataTable[DataName_Str]
        else
            return nil
        end
    end
end
function npc_everything_data:Get(DataName_Str)
    return self:ReadData(DataName_Str)
end
function npc_everything_data:Set(DataName_Str,theData)
    self:SaveData(DataName_Str, theData)
end

function npc_everything_data:Add(DataName_Str,num)
    if self:Get(DataName_Str) == nil then
        self:Set(DataName_Str, 0)
    end
    if type(num) ~= "number" or type(self:Get(DataName_Str))~="number" then
        return
    end
    self:Set(DataName_Str, self:Get(DataName_Str) + num)
    return self:Get(DataName_Str)
end
------------------------------------------------------------------------------------------------------------------------------
-- -- 用于NPC 刷新 队列相关保存操作 pop
function npc_everything_data:Save_Spawn_CMD_Table(_table)   ---- 保存去队列
    local current_table = self:Get("NPC_SPAWN_QUEUE") or {}
    table.insert(current_table,_table)
    self:Set("NPC_SPAWN_QUEUE",current_table)
end

function npc_everything_data:Get_Spawn_CMD_Table(no_pop)
    local current_table = self:Get("NPC_SPAWN_QUEUE")
    if current_table == nil then
        return nil
    end
    
    local ret_table = current_table[1]
    local new_table = {}
    if #current_table > 1 then
        for i, v in ipairs(current_table) do
            if i ~= 1 and v then
                table.insert(new_table,v)
            end
        end
    end
    if no_pop ~= true then
        self:Set("NPC_SPAWN_QUEUE",new_table)
    end
    return ret_table
end

function npc_everything_data:Clean_Spawn_CMD_Table()
    self:Set("NPC_SPAWN_QUEUE",{})
end

function npc_everything_data:Get_Spawn_Queue_Num()
    local temp = self:Get("NPC_SPAWN_QUEUE") or {}
    return #temp
end
------------------------------------------------------------------------------------------------------------------------------
---- 时间队列
function npc_everything_data:Save_Spwan_Time(time)
    local current_time_table = self:Get("NPC_SPAWN_QUEUE_TIME") or {}
    table.insert(current_time_table,time)
    self:Set("NPC_SPAWN_QUEUE_TIME",current_time_table)
end

function npc_everything_data:Get_Spawn_Time(no_pop)
    local current_table = self:Get("NPC_SPAWN_QUEUE_TIME")
    if current_table == nil then
        return 500  --- 如果为空，则返回超过一天的时间数，避免今天刷新
    end
    local ret_time = current_table[1]
    local new_table = {}
    if #current_table > 1 then
        for i, v in ipairs(current_table) do
            if i ~= 1 and v then
                table.insert(new_table,v)
            end
        end
    end
    if no_pop ~= true then
        self:Set("NPC_SPAWN_QUEUE_TIME", new_table)
    end
    return (ret_time or 500)
end

function npc_everything_data:Clean_Spawn_Time()
    self:Set("NPC_SPAWN_QUEUE_TIME", {})
end

------------------------------------------------------------------------------------------------------------------------------
------ 成就系统，给 player 自身执行，同步world 和自身数据
function npc_everything_data:Achievements_Set(Ach_name,data)
    if Ach_name == nil or data == nil or not self.inst:HasTag("player") then
        return
    end
    local userid = self.inst.userid
    local player_ach_talbe = self:Get("Achievement_Table") or {}
    player_ach_talbe[Ach_name] = data
    local os_time = os.time()
    self:Set("Achievement_Table",player_ach_talbe)
    self:Set("Achievement_Table.time",os_time)
    TheWorld.npc_everything_data:Set("Achievement_Table."..userid,player_ach_talbe)
    TheWorld.npc_everything_data:Set("Achievement_Table.time."..userid,os_time)
end
function npc_everything_data:Achievements_Get(Ach_name)
    if  Ach_name == nil or not self.inst:HasTag("player") then
        return {}
    end
    -- local userid = self.inst.userid
    local player_ach_talbe = self:Get("Achievement_Table") or {}
    local ret_talbe = player_ach_talbe[Ach_name] or {}
    return ret_talbe
end
function npc_everything_data:Achievements_Synchronize_Update_By_Player()
    if not self.inst:HasTag("player") then
        return
    end
    local userid = self.inst.userid
    local time_in_player = self:Get("Achievement_Table.time") or 0
    local time_in_world = TheWorld.npc_everything_data:Get("Achievement_Table.time."..userid) or 0
    local table_in_player = self:Get("Achievement_Table") or {}
    local talbe_in_world = TheWorld.npc_everything_data:Get("Achievement_Table."..userid) or {}

    if time_in_player > time_in_world then
        -- print("time_in_player > time_in_world")
        TheWorld.npc_everything_data:Set("Achievement_Table."..userid,table_in_player)
        TheWorld.npc_everything_data:Set("Achievement_Table.time."..userid,time_in_player)
    else
        -- print("time_in_player < time_in_world")
        self:Set("Achievement_Table",talbe_in_world)
        self:Set("Achievement_Table.time",time_in_world)
    end
end
------------------------------------------------------------------------------------------------------------------------------
function npc_everything_data:OnSave()
    local data =
    {
        DataTable = self.DataTable
    }

    return next(data) ~= nil and data or nil
end

function npc_everything_data:OnLoad(data)
    if data.DataTable then
        self.DataTable = data.DataTable
    end
end




return npc_everything_data